package org.smartboot.flow.springboot.extension;

import org.smartboot.flow.manager.change.HttpManager;
import org.smartboot.flow.manager.reload.Reloader;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author huqiang
 * @since 2022/12/14 19:15
 */
@Configuration
@ConfigurationProperties(prefix = "smart.flow.manage.http")
@ConditionalOnProperty(name = "smart.flow.manage.http.url")
public class FlowHttpManageConfiguration {

    /**
     * Connect timeout in microseconds.
     */
    private long timeout;

    /**
     * Manage request url.
     */
    private String url;

    /**
     * request idle in microseconds.
     */
    private long idle;

    /**
     * First delay in microseconds.
     */
    private long delayAtFirst;

    /**
     * Configuration http manager without reloader.
     */
    @Bean(value = "smart-flow-http-manager", initMethod = "start")
    @ConditionalOnMissingBean(Reloader.class)
    public HttpManager getHttpManager(){
        HttpManager httpManager = new HttpManager();
        httpManager.setTimeout(timeout);
        httpManager.setUrl(url);
        httpManager.setIdle(idle);
        httpManager.setDelayAtFirst(delayAtFirst);
        return httpManager;
    }

    /**
     * Configuration http manager with reloader.
     */
    @Bean(value = "smart-flow-http-manager", initMethod = "start")
    @ConditionalOnBean(Reloader.class)
    public HttpManager getHttpManager(Reloader reloader){
        HttpManager httpManager = new HttpManager();
        httpManager.setTimeout(timeout);
        httpManager.setUrl(url);
        httpManager.setIdle(idle);
        httpManager.setDelayAtFirst(delayAtFirst);
        httpManager.setReloader(reloader);
        return httpManager;
    }


    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setIdle(long idle) {
        this.idle = idle;
    }

    public void setDelayAtFirst(long delayAtFirst) {
        this.delayAtFirst = delayAtFirst;
    }
}
